package com.gt.wxpay.cashier.payment.interfaces.service;

import com.gt.wxpay.cashier.application.PaymentRemoteService;
import com.gt.wxpay.cashier.domain.Result;
import com.gt.wxpay.cashier.domain.dto.PayRespDto;
import com.gt.wxpay.cashier.domain.dto.QueryOrderDto;
import com.gt.wxpay.cashier.domain.model.CashierPayment;
import com.gt.wxpay.cashier.payment.application.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 支付记录接口
 *
 * @author cztech
 */
@Slf4j
@RestController
@RequestMapping("/service/cashier/payOrder")
public class PaymentServiceController implements PaymentRemoteService {
    @Resource
    private PaymentService paymentService;

    /**
     * 下单接口
     *
     * @param cashierPayment CashierPayment
     * @return
     */
    @RequestMapping("/create")
    @Override
    public Result<PayRespDto> create(CashierPayment cashierPayment) {
        cashierPayment.setStateId(1);
        cashierPayment.setCreator(cashierPayment.getPhoneNo());
        cashierPayment.setCreatorId(Long.parseLong(cashierPayment.getUserId()));
        PayRespDto order = null;
        try {
            order = paymentService.order(cashierPayment);
        } catch (Exception e) {
            log.error("支付下单失败", e);
            return Result.error(e.getMessage());
        }
        Result<PayRespDto> result = Result.ok();
        result.setResponse(order);
        return result;
    }

    @GetMapping("/queryPayInfo")
    @Override
    public Result<CashierPayment> queryByPamentNo(@RequestParam String paymentNo) {
        CashierPayment cashierPayment = null;
        try {
             cashierPayment = paymentService.queryByPamentNo(paymentNo);
        } catch (Exception e) {
            log.error("支付查询失败", e);
            return Result.error(e.getMessage());
        }
        Result<CashierPayment> ok = Result.ok();
        ok.setResponse(cashierPayment);
        return ok;
    }

    @GetMapping("/query")
    @Override
    public Result<QueryOrderDto> queryPayInfo(String paymentNo) {
        QueryOrderDto queryOrderDto = null;
        try {
            queryOrderDto = paymentService.queryInfo(paymentNo);
        } catch (Exception e) {
            log.error("退款查询微信失败", e);
            return Result.error(e.getMessage());
        }
        Result<QueryOrderDto> ok = Result.ok();
        ok.setResponse(queryOrderDto);
        return ok;
    }

    @GetMapping("/close")
    @Override
    public Result<String> close(@RequestParam String paymentNo) {
        Result result;
        try {
           result = paymentService.closeOrder(paymentNo);
        } catch (Exception e) {
            log.error("支付关闭失败", e);
            return Result.error(e.getMessage());
        }
        return result;
    }
}
